package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.UserMapper;
import cn.tedu.coolshark.pojo.dto.UserDTO;
import cn.tedu.coolshark.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/login")
    public int login(@RequestBody UserDTO userDTO,HttpSession session){

        UserVO userVO = mapper.selectByUsername(userDTO.getUsername());
        if (userVO != null) {
            if (userVO.getPassword().equals(userDTO.getPassword())) {
                session.setAttribute("userVO",userVO);
                return 1;
            }
            return 2;
        }
        return 3;
    }
    @RequestMapping("/currentUser")
    public UserVO currentUser(HttpSession session){
        return (UserVO)session.getAttribute("userVO");
    }
    @RequestMapping("/exit")
    public void exit(HttpSession session){
        session.removeAttribute("userVO");
    }
}
